#include "stdafx.h"
#include "../client/DCPlusPlus.h"
#include "Resource.h"

#include "FakeSharesPage.h"
#include "FakeSharesDlg.h"

#include "../client/SettingsManager.h"
#include "../client/ClientProfileManager.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

PropPage::TextItem FakeSharesPage::texts[] = {
	{ IDC_ADD_FAKE_SHARE, ResourceManager::ADD },
	{ IDC_CHANGE_FAKE_SHARE, ResourceManager::SETTINGS_CHANGE },
	{ IDC_REMOVE_FAKE_SHARE, ResourceManager::REMOVE },
	{ 0, ResourceManager::SETTINGS_AUTO_AWAY }
};

PropPage::Item FakeSharesPage::items[] = {
	{ IDC_PERCENT_FAKE_SHARE_TOLERATED, SettingsManager::PERCENT_FAKE_SHARE_TOLERATED, PropPage::T_INT_SHOWDEFAULT},
	{ IDC_MINIMUM_FILELIST_SIZE, SettingsManager::MINIMUM_FILELIST_SIZE, PropPage::T_INT_SHOWDEFAULT },
	{ IDC_MAXIMUM_FILELIST_SIZE, SettingsManager::MAXIMUM_FILELIST_SIZE, PropPage::T_INT_SHOWDEFAULT },
	{ 0, 0, PropPage::T_END }
};

LRESULT FakeSharesPage::onInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	PropPage::translate((HWND)(*this), texts);
	PropPage::read((HWND)*this, items);

	CRect rc;

	ctrlFakeShares.Attach(GetDlgItem(IDC_FAKE_SHARE_ITEMS));
	ctrlFakeShares.GetClientRect(rc);

	ctrlFakeShares.InsertColumn(0, CTSTRING(SETTINGS_VALUE), LVCFMT_LEFT, rc.Width() / 2, 0);
	ctrlFakeShares.InsertColumn(1, CTSTRING(TYPE), LVCFMT_LEFT, rc.Width() / 2, 1);

	if(BOOLSETTING(FULL_ROW_SELECT))
		ctrlFakeShares.SetExtendedListViewStyle(LVS_EX_FULLROWSELECT);

	// Do specialized reading here

	FakeShares::List& ul = ClientProfileManager::getInstance()->getFakeShares();
	StringList cols;
	for(FakeShares::Iter j = ul.begin(); j != ul.end(); ++j) {
		cols.push_back(j->getValue());
		cols.push_back(j->getType());
		ctrlFakeShares.insert(cols);
		cols.clear();
	}

	CComboBox cRaw;
	cRaw.Attach(GetDlgItem(IDC_FAKE_RAW));
	for(int n=0;n<11;){
		cRaw.AddString(RAWSETTING(n).c_str());
		++n;
	}
	cRaw.SetCurSel(SETTING(FAKE_RAW));
	cRaw.Detach();

	return TRUE;
}

LRESULT FakeSharesPage::onAddFakeShare(WORD , WORD , HWND , BOOL& ) {
	FakeSharesDlg dlg;

	if(dlg.DoModal() == IDOK) {
		StringList lst;
		lst.push_back(dlg.value);
		lst.push_back(dlg.type);
		ctrlFakeShares.insert(lst);
	}
	return 0;
}

LRESULT FakeSharesPage::onChangeFakeShare(WORD , WORD , HWND , BOOL& ) {
	
#define BUFLEN 256
	if(ctrlFakeShares.GetSelectedCount() == 1) {
		int sel = ctrlFakeShares.GetSelectedIndex();
		TCHAR buf[BUFLEN];
		FakeSharesDlg dlg;
		ctrlFakeShares.GetItemText(sel, 0, buf, BUFLEN);
		dlg.value = buf;
		ctrlFakeShares.GetItemText(sel, 1, buf, BUFLEN);
		dlg.type = buf;

		if(dlg.DoModal() == IDOK) {
			ctrlFakeShares.SetItemText(sel, 0, dlg.value.c_str());
			ctrlFakeShares.SetItemText(sel, 1, dlg.type.c_str());
		}
	}
	return 0;
}

LRESULT FakeSharesPage::onRemoveFakeShare(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
	if(ctrlFakeShares.GetSelectedCount() == 1) {
		ctrlFakeShares.DeleteItem(ctrlFakeShares.GetNextItem(-1, LVNI_SELECTED));
	}
	return 0;
}

void FakeSharesPage::write() {
	int it = ctrlFakeShares.GetItemCount();
#define BUFLEN 256
	TCHAR buf[BUFLEN];
	tstring value, type;
	FakeShares::List& ul = ClientProfileManager::getInstance()->getFakeShares();
	ul.clear();
	for(int i = 0; i < it; ++i) {
		ctrlFakeShares.GetItemText(i, 0, buf, BUFLEN);
		value = buf;
		ctrlFakeShares.GetItemText(i, 1, buf, BUFLEN);
		type = buf;
		ul.push_back(FakeShares(value, type));
	}
	CComboBox cRaw(GetDlgItem(IDC_FAKE_RAW));
	SettingsManager::getInstance()->set(SettingsManager::FAKE_RAW, cRaw.GetCurSel());
	ClientProfileManager::getInstance()->saveFakeShares();
	PropPage::write((HWND)*this, items);
}
/**
 * @file
 * $Id: ClientsPage.cpp,v 1.0 2004/02/22 14:06 psf8500 Exp $
 */